package com.ling.zookeeper.zookeeperUtil;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 * 
 * @author yueling
 * 界面布局
 */
public class Uidesin extends JFrame{

	/**
	 * 创建前缀提示label
	 */
	public JLabel srcZklabel=new JLabel();
	public JLabel srcNodeLabel=new JLabel();
	public JLabel destZklabel=new JLabel();
	public JLabel destNodeLabel=new JLabel();
	
	public JLabel zklabel=new JLabel();
	public JLabel zkNodeLabel=new JLabel();
	public JLabel zkNodeDataLabel=new JLabel();
	
	
	/**
	 * 创建结果展示label
	 */
	public JLabel srcZkResultlabel=new JLabel();
	public JLabel srcNodeResultLabel=new JLabel();
	public JLabel destZkResultlabel=new JLabel();
	public JLabel destNodeResultLabel=new JLabel();
	
	public JLabel deleteZkResultlabel=new JLabel();
	public JLabel deleteNodeResultLabel=new JLabel();
	
	/**
	 * 创建信息显示label
	 */
	public JLabel infoLabel=new JLabel();
	
	/**
	 * 创建按钮
	 */
	public JButton buttonOk=new JButton("");
	public JButton buttondelete=new JButton("");
	public JButton buttonQuery=new JButton("");
	public JButton buttonCreate=new JButton("");
	
	/**
	 * 创建文本框
	 */
	public JTextField srcZkText=new JTextField();
	public JTextField srcNodeText=new JTextField();
	public JTextField destZkText=new JTextField();
	public JTextField destNodeText=new JTextField();
	public JTextField infoText=new JTextField();
	
	public JTextField zkText=new JTextField();
	public JTextField zkNodeText=new JTextField();
	public JTextField nodeData=new JTextField();

	/**
	 * 构造函数
	 */
	public Uidesin() {
		super();
		setSize(700, 600);
		
		getContentPane().setLayout(null);// 设置布局控制器
		/**
		 * 添加复制配置信息……
		 */
		this.add(this.getLabel(this.srcZklabel, "源zk地址+端口", 40, 50, 110, 30), null);// 添加标签
		this.add(this.getLabel(this.srcNodeLabel, "源节点路径", 40, 90, 110, 30), null);// 添加标签
		this.add(this.getLabel(this.destZklabel, "目标zk地址+端口", 40, 135, 110, 30), null);// 添加标签
		this.add(this.getLabel(this.destNodeLabel, "目标节点路径", 40, 175, 110, 30), null);// 添加标签
		
		this.add(this.getTextField(this.srcZkText, "src_ip:port //只检查连通性，不检查zk服务是否存在，自己保证~~", 155, 50, 500, 30), null);// 添加文本框
		this.add(this.getTextField(this.srcNodeText, "/path/path", 155, 90, 500, 30), null);// 添加文本框
		this.add(this.getTextField(this.destZkText, "dest_ip:port  //只检查连通性，不检查zk服务是否存在，自己保证~~", 155, 135, 500, 30), null);// 添加文本框
		this.add(this.getTextField(this.destNodeText, "/pathx/pathy", 155, 175, 500, 30), null);// 添加文本框

	
		this.add(this.getButton(buttonOk,"开始复制",155, 215, 500, 30), null);// 添加按钮
		/**
		 * 添加信息展示区域
		 */
		this.add(this.getLabel(this.infoLabel, "提示信息……", 155, 255, 500, 100), null);// 添加标签
		this.infoLabel.setVerticalAlignment(JLabel.TOP);
		this.infoLabel.setBorder(BorderFactory.createLineBorder(Color.red));
		//this.add(this.getTextField(this.infoText, "提示信息……", 155, 255, 500, 110), null);// 添加文本框
		
		/**
		 * 添加创建、删除、查询的配置信息
		 */
		this.add(this.getLabel(this.zklabel, "zk地址+端口", 40, 365, 110, 30), null);// 添加标签
		this.add(this.getLabel(this.zkNodeLabel, "节点路径", 40, 405, 110, 30), null);// 添加标签
		this.add(this.getLabel(this.zkNodeDataLabel, "节点数据", 40, 445, 110, 30), null);// 添加标签
		
		this.add(this.getTextField(this.zkText, "src_ip:port  //只检查连通性，不检查zk服务是否存在，自己保证~~", 155, 365, 500, 30), null);// 添加文本框
		this.add(this.getTextField(this.zkNodeText, "/pathx/pathy 删除/更新时慎用！慎用！删错/覆盖 自己撞墙~~", 155, 405, 500, 30), null);// 添加文本框
		this.add(this.getTextField(this.nodeData, "创建/更新时使用，删除和查询可以不设置~", 155, 445, 500, 30), null);// 添加文本框
		/**
		 * 添加创建、删除、查询按钮
		 */
		this.add(this.getButton(buttondelete,"删除",155, 485, 150, 30), null);// 添加按钮
		this.add(this.getButton(buttonCreate,"创建/更新",330, 485, 150, 30), null);// 添加按钮
		this.add(this.getButton(buttonQuery,"查询",505, 485, 150, 30), null);// 添加按钮
		
		/**
		 * 添加结果展示label
		 */
		this.add(this.getLabel(this.srcZkResultlabel, "", 665, 50, 30, 30), null);// 添加标签
		this.add(this.getLabel(this.srcNodeResultLabel, "", 665, 90, 30, 30), null);// 添加标签
		this.add(this.getLabel(this.destZkResultlabel, "", 665, 135, 30, 30), null);// 添加标签
		this.add(this.getLabel(this.destNodeResultLabel, "", 665, 175, 30, 30), null);// 添加标签
		
		this.add(this.getLabel(this.deleteZkResultlabel, "", 665, 365, 30, 30), null);// 添加标签
		this.add(this.getLabel(this.deleteNodeResultLabel, "", 665, 405, 30, 30), null);// 添加标签
		
		/**
		 * 添加文本的焦点事件
		 */
		addFocusEvent(srcZkText);
		addFocusEvent(srcNodeText);
		addFocusEvent(destZkText);
		addFocusEvent(destNodeText);
		addFocusEvent(infoText);

		addFocusEvent(zkText);
		addFocusEvent(zkNodeText);
		addFocusEvent(nodeData);
		
		setTitle("zookeeper copy&delete @yueling");// 设置窗口标题

	}

	/**
	 * 设置标签
	 * 
	 * @return 设置好的标签
	 */
	private JLabel getLabel(JLabel jLabel, String defaultText, int x, int y, int width, int height) {
		if (jLabel == null) {
			jLabel = new JLabel();
			jLabel.setBounds(x, y, width, height);
			jLabel.setText(defaultText);
			jLabel.setToolTipText(defaultText);
		}else {
			jLabel.setBounds(x, y, width, height);
			jLabel.setText(defaultText);
			jLabel.setToolTipText(defaultText);
		}
		return jLabel;
	}

	/**
	 * 设置按钮
	 * 
	 * @return 设置按钮
	 */
	private JButton getButton(JButton jButton,String defaultText, int x, int y, int width, int height) {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(x, y, width, height);
			jButton.setText(defaultText);
			jButton.setToolTipText(defaultText);
		}else {
			jButton.setBounds(x, y, width, height);
			jButton.setText(defaultText);
			jButton.setToolTipText(defaultText);
		}
		return jButton;
	}

	/**
	 * 设定文本域
	 * 
	 * @return
	 */
	private JTextField getTextField(JTextField jTextField, String defaultText, int x, int y, int width, int height) {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(x, y, width, height);
			jTextField.setText(defaultText);
			jTextField.setToolTipText(defaultText);
		}else{
			jTextField.setBounds(x, y, width, height);
			jTextField.setText(defaultText);
			jTextField.setToolTipText(defaultText);
		}
		return jTextField;
	}
	/**
	 * 添加文本区域的获取焦点事件
	 * @param jTextField
	 */
	private void addFocusEvent(JTextField jTextField){
		
		jTextField.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				jTextField.selectAll();
			}
		});
	}
}